namespace DotNetNuke.Common.Utilities
{
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Tabs;
    using DotNetNuke.Services.Cache;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Web.Caching;

    public class DataCache
    {
        [Obsolete("This method is obsolete. Use the new specific methods: ClearHostCache, ClearPortalCache, ClearTabCache.")]
        public static void ClearCoreCache(CoreCacheType Type, [Optional, DefaultParameterValue(-1)] int ID, [Optional, DefaultParameterValue(false)] bool Cascade)
        {
            switch (((int) Type))
            {
                case 1:
                    ClearHostCache(Cascade);
                    break;

                case 2:
                    ClearPortalCache(ID, Cascade);
                    break;

                case 3:
                {
                    TabInfo objTab = new TabController().GetTab(ID);
                    if (objTab != null)
                    {
                        ClearTabCache(ID, objTab.PortalID);
                    }
                    break;
                }
            }
        }

        public static void ClearHostCache(bool Cascade)
        {
            RemoveCache("GetHostSettings");
            RemoveCache("GetPortalByAlias");
            RemoveCache("CSS");
            if (Cascade)
            {
                ArrayList arrPortals = new PortalController().GetPortals();
                int refIntHelperL0 = arrPortals.Count - 1;
                for (int intIndex = 0; intIndex <= refIntHelperL0; intIndex++)
                {
                    PortalInfo objPortal = (PortalInfo) arrPortals[intIndex];
                    ClearPortalCache(objPortal.PortalID, Cascade);
                }
            }
        }

        public static void ClearPortalCache(int PortalId, bool Cascade)
        {
            ArrayList arrTabs = (ArrayList) GetCache("GetTabs" + PortalId.ToString());
            RemovePersistentCacheItem("GetPortalSettings" + PortalId.ToString());
            RemoveCache("GetTabs" + PortalId.ToString());
            if (Cascade)
            {
                if (arrTabs == null)
                {
                    arrTabs = new TabController().GetTabs(PortalId);
                }
                int refIntHelperL0 = arrTabs.Count - 1;
                for (int intIndex = 0; intIndex <= refIntHelperL0; intIndex++)
                {
                    TabInfo objTab = (TabInfo) arrTabs[intIndex];
                    ClearTabCache(objTab.TabID);
                }
                RemoveCache("GetTabPermissionsByPortal" + PortalId.ToString());
            }
        }

        private static void ClearTabCache(int TabId)
        {
            RemoveCache("GetTab" + TabId.ToString());
            RemoveCache("GetPortalTabModules" + TabId.ToString());
        }

        public static void ClearTabCache(int TabId, int PortalId)
        {
            ClearTabCache(TabId);
            RemoveCache("GetTabPermissionsByPortal" + PortalId.ToString());
        }

        public static object GetCache(string CacheKey)
        {
            return CachingProvider.Instance().GetItem(CacheKey);
        }

        public static object GetPersistentCacheItem(string CacheKey, Type objType)
        {
            return CachingProvider.Instance().GetPersistentCacheItem(CacheKey, objType);
        }

        public static void RemoveCache(string CacheKey)
        {
            CachingProvider.Instance().Remove(CacheKey);
        }

        public static void RemovePersistentCacheItem(string CacheKey)
        {
            CachingProvider.Instance().RemovePersistentCacheItem(CacheKey);
        }

        public static void SetCache(string CacheKey, object objObject)
        {
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(objObject), false);
        }

        public static void SetCache(string CacheKey, object objObject, bool PersistAppRestart)
        {
            CachingProvider.Instance().Insert(CacheKey, RuntimeHelpers.GetObjectValue(objObject), PersistAppRestart);
        }

        public static void SetCache(string CacheKey, object objObject, DateTime AbsoluteExpiration)
        {
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(objObject), AbsoluteExpiration, false);
        }

        public static void SetCache(string CacheKey, object objObject, TimeSpan SlidingExpiration)
        {
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(objObject), SlidingExpiration, false);
        }

        public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency)
        {
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(objObject), objDependency, false);
        }

        public static void SetCache(string CacheKey, object objObject, DateTime AbsoluteExpiration, bool PersistAppRestart)
        {
            CachingProvider.Instance().Insert(CacheKey, RuntimeHelpers.GetObjectValue(objObject), null, AbsoluteExpiration, Cache.NoSlidingExpiration, PersistAppRestart);
        }

        public static void SetCache(string CacheKey, object objObject, TimeSpan SlidingExpiration, bool PersistAppRestart)
        {
            CachingProvider.Instance().Insert(CacheKey, RuntimeHelpers.GetObjectValue(objObject), null, Cache.NoAbsoluteExpiration, SlidingExpiration, PersistAppRestart);
        }

        public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency, bool PersistAppRestart)
        {
            CachingProvider.Instance().Insert(CacheKey, RuntimeHelpers.GetObjectValue(objObject), objDependency, PersistAppRestart);
        }

        public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency, DateTime AbsoluteExpiration, TimeSpan SlidingExpiration)
        {
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(objObject), objDependency, AbsoluteExpiration, SlidingExpiration, false);
        }

        public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency, DateTime AbsoluteExpiration, TimeSpan SlidingExpiration, bool PersistAppRestart)
        {
            CachingProvider.Instance().Insert(CacheKey, RuntimeHelpers.GetObjectValue(objObject), objDependency, AbsoluteExpiration, SlidingExpiration, PersistAppRestart);
        }

        public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency, DateTime AbsoluteExpiration, TimeSpan SlidingExpiration, CacheItemPriority Priority, CacheItemRemovedCallback OnRemoveCallback)
        {
            SetCache(CacheKey, RuntimeHelpers.GetObjectValue(objObject), objDependency, AbsoluteExpiration, SlidingExpiration, Priority, OnRemoveCallback, false);
        }

        public static void SetCache(string CacheKey, object objObject, CacheDependency objDependency, DateTime AbsoluteExpiration, TimeSpan SlidingExpiration, CacheItemPriority Priority, CacheItemRemovedCallback OnRemoveCallback, bool PersistAppRestart)
        {
            CachingProvider.Instance().Insert(CacheKey, RuntimeHelpers.GetObjectValue(objObject), null, Cache.NoAbsoluteExpiration, SlidingExpiration, PersistAppRestart);
        }

        public static bool CachePersistenceEnabled
        {
            get
            {
                return ((Config.GetSetting("EnableCachePersistence") != null) && (Config.GetSetting("EnableCachePersistence") == "true"));
            }
        }
    }
}

